<template>
  <ea-dialog
    v-bind="$attrs"
    v-on="$listeners"
    width="600px"
    ref="elDialog"
    :title="title"
    :type="type"
    @submit="submit"
    @reset="resetForm"
    @closed="end"
  >
    <el-form ref="form" :model="singleData" :rules="rules" label-width="100px">
      <el-form-item label="班级" prop="classCode" v-if="adminId != '2007'">
        <!-- :key="new Date().getTime()" -->
        <el-cascader
          :options="deptClass"
          v-model="singleData.classCode"
          :show-all-levels="true"
          placeholder="请选择班级"
          ref="cascaderAddr"
          collapse-tags
          @change="orderChange"
          :props="{
            multiple: true,
            value: 'code',
            label: 'name',
            emitPath: false
          }"
          clearable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="标题" prop="name">
        <el-input
          v-model="singleData.name"
          maxlength="50"
          show-word-limit
          placeholder="请输入通知标题"
        />
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input
          type="textarea"
          placeholder="请输入内容"
          v-model="singleData.content"
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="直接发布" prop="state">
        <el-switch
          v-model="singleData.state"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </el-form-item>
    </el-form>
  </ea-dialog>
</template>

<script>
import editAndAdd from "../../mixins/business/editAndAdd";

export default {
  name: "EaNews",
  mixins: [editAndAdd],
  data() {
    return {
      moduleName: "公告",
      singleData: {
        classCode: "",
        content: "",
        name: "",
        noticeid: null,
        state: 1
      },
      rules: {
        classCode: { required: true, message: "请选择学生！" },
        name: { required: true, message: "请输入通知标题！" },
        content: { required: true, message: "请输入通知内容！" }
      },
      deptClass: [], //班级
      adminId: ""
    };
  },
  computed: {
    isTeacher() {
      return this.moduleName === "教师";
    }
  },
  created() {
    this.adminId = this.$store.state.user.userid;
    this.getDepAndClass();
  },
  methods: {
    resetForm() {
      this.singleData = {
        classCode: "",
        content: "",
        name: "",
        noticeid: null,
        state: 1
      };
      this.$refs.form.resetFields();
    },
    orderChange(e) {
      // const deepArr = this.deptClass;
      // 学员CODE一起传入
      // for (var j = 0; j < deepArr.length; j++) {
      //   if (!deepArr[j].children) {
      //     return;
      //   }
      //   for (var i = 0; i < deepArr[j].children.length; i++) {
      //     if (deepArr[j].children[i].code == this.singleData.classCode[0]) {
      //       // this.singleData.classCode.push(deepArr[j].code);
      //     }
      //   }
      // }
    },
    getDepAndClass() {
      this.axios.get(`/sys/notice/getStudent`).then(res => {
        this.deptClass = res;
      });
    },
    handle() {
      if (this.singleData.classCode) {
        this.singleData.studentid = this.singleData.classCode.join(",");
        this.singleData.stuname = this.singleData.studentid;
        delete this.singleData["classCode"];
        delete this.singleData["examid"];
      }
      return this.axios.post("/sys/notice/saveOrUpdate", this.singleData);
    }
  }
};
</script>

<style scoped></style>
